package com.xintonghua.bussiness.ui.fragment.cube;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xintonghua.base.utils.DividerGridItemDecoration;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.StockAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CubeBean;
import com.xintonghua.bussiness.bean.Time;
import com.xintonghua.bussiness.bean.TurnoverBean;
import com.xintonghua.bussiness.ui.fragment.client.user.ConsumeActivity;
import com.xintonghua.bussiness.ui.fragment.cube.turnover.ServiceOrProductListActivity;
import com.xintonghua.bussiness.ui.fragment.cube.turnover.TurnoverActivity;
import com.xintonghua.bussiness.util.CalendarTools;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.ListUtil;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.TimeCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class TimeSaiXuanActivity extends BaseActivity implements initData, Observer {
    @BindView(R.id.lv_come_in)
    RecyclerView lvComeIn;

    @BindView(R.id.lv_come_in2)
    RecyclerView lvComeIn2;
    Unbinder unbinder;
    @BindView(R.id.tv_top_yingyee)
    TextView tvTopYingyee;
    @BindView(R.id.tv_all_get)
    TextView tvAllGet;
    @BindView(R.id.fl_see_more)
    FrameLayout flSeeMore;
    @BindView(R.id.fl_see_more2)
    FrameLayout flSeeMore2;
    @BindView(R.id.fl_see_more3)
    FrameLayout flSeeMore3;
    @BindView(R.id.tv_all_zhichu)
    TextView tvAllZhichu;
    @BindView(R.id.iv_normal)
    ImageView ivNormal;
    @BindView(R.id.tv_normal)
    TextView tvNormal;
    @BindView(R.id.rl_normal)
    RelativeLayout rlNormal;
    @BindView(R.id.iv_no)
    ImageView ivNo;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.rl_no)
    RelativeLayout rlNo;
    Unbinder unbinder1;
    @BindView(R.id.tv_jinghuoxiaofei)
    TextView tvJinghuoxiaofei;
    @BindView(R.id.tv_time_saixuan)
    TextView tvTimeSaixuan;

    @BindView(R.id.fl_select_time)
    FrameLayout flSelectTime;
    @BindView(R.id.tv_select_times)
    TextView tvSelectTimes;

    Time time;
    private List<CubeBean> cubeBeanList;
    private List<CubeBean> cubeBeanList2;

    private List<CubeBean> cubeBeanList3;

    @BindView(R.id.sv_scrollview)
    NestedScrollView svScrollview;
    @BindView(R.id.lv_out_money)
    RecyclerView lvOutMoney;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_sai_xuan);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("时间筛选");
        time = new Time();
        time.addObserver(this);
        flSelectTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JumpUtils.jumpActivityForResult(TimeSaiXuanActivity.this, DialogStartAndEndTimeActivity.class, null, MyState.requestOne);
            }
        });
        initUI();
    }

    String starttime = "";
    String endtime = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestOne && resultCode == Activity.RESULT_OK) {

            if (data != null) {
                starttime = data.getStringExtra("starttime");
                endtime = data.getStringExtra("endtime");

                tvSelectTimes.setText(starttime + " 至 " + endtime);
                svScrollview.setVisibility(View.VISIBLE);
                getNetData();
            }
        }
    }

    public void initUI() {
        unbinder = ButterKnife.bind(this);
        cubeBeanList = new ArrayList<>();
        cubeBeanList2 = new ArrayList<>();
        cubeBeanList3 = new ArrayList<>();
//        getNetData();
//        cubeBeanList.add(new CubeBean(R.drawable.icon_skin, "皮肤", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_health, "健康", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_stature, "身材", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_high_tech_beauty, "高端仪器", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.cube_product, "产品一", "" + 200, "" + 10, false));
//        cubeBeanList.add(new CubeBean(R.drawable.cube_product, "产品二", "" + 200, "" + 10, false));
//        cubeBeanList.add(new CubeBean(R.drawable.cube_product, "产品三", "" + 200, "" + 10, false));
//        cubeBeanList.add(new CubeBean(R.drawable.cube_product, "产品四", "" + 200, "" + 10, false));

    }

    @Override
    public void getNetData() {
        httpCent.getDataStatics(getIntent().getIntExtra(BuildConfig.FLAVOR, 0), this, starttime, endtime, MyState.requestOne);
    }

    @Override
    public List<CubeBean> ServiceListBeantoCubeBean(List<TurnoverBean.ServiceListBean> beans) {
        List_img = new ArrayList<>();
        List_img.add(R.drawable.icon_skin);
        List_img.add(R.drawable.icon_health);
        List_img.add(R.drawable.icon_stature);
        List_img.add(R.drawable.icon_high_tech_beauty);
//        cubeBeanList.add(new CubeBean(R.drawable.icon_skin, "皮肤", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_health, "健康", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_stature, "身材", "" + 2000, "" + 0, true));
//        cubeBeanList.add(new CubeBean(R.drawable.icon_high_tech_beauty, "高端仪器", "" + 2000, "" + 0, true)
        List<CubeBean> list2 = new ArrayList<>();
        if (!ListUtil.isNullOrEmpty(beans))
            for (int i = 0; i < beans.size(); i++) {
                try {
                    CubeBean bean = new CubeBean(List_img.get(i), beans.get(i).getId(), beans.get(i).getName(), beans.get(i).getTotal(), beans.get(i).getCount(), false);
                    list2.add(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    CubeBean bean = new CubeBean(List_img.get(0), beans.get(i).getId(), beans.get(i).getName(), beans.get(i).getTotal(), beans.get(i).getCount(), false);
                    list2.add(bean);
                }
            }
        return list2;
    }

    @Override
    public List<CubeBean> ProjectlistBeantoCubeBean(List<TurnoverBean.ProjectlistBean> beans) {
        List<CubeBean> list = new ArrayList<>();
        if (!ListUtil.isNullOrEmpty(beans))
            for (int i = 0; i < beans.size(); i++) {
//            cubeBeanList.add(new CubeBean(R.drawable.icon_skin, "皮肤", "" + 2000, "" + 0, true));
                CubeBean bean = new CubeBean(R.drawable.cube_product, beans.get(i).getId(), beans.get(i).getName(), beans.get(i).getTotal(), beans.get(i).getCount(), false);
                list.add(bean);
            }
        return list;
    }

    @Override
    public List<CubeBean> ZhiChulistBeantoCubeBean(List<TurnoverBean.ZhiChuListBean> beans) {
        List<CubeBean> list = new ArrayList<>();
        if (!ListUtil.isNullOrEmpty(beans))
            for (int i = 0; i < beans.size(); i++) {
                CubeBean bean = new CubeBean(R.drawable.icon_refund, beans.get(i).getId(), beans.get(i).getTitle(), beans.get(i).getMoney(), "", false);
                list.add(bean);
            }
        return list;
    }

    @Override
    public void initRecy0Service(List<CubeBean> listservice) {
        final StockAdapter adapter = new StockAdapter(TimeSaiXuanActivity.this, listservice);
        adapter.setType(0);
        lvComeIn.addItemDecoration(new DividerGridItemDecoration(TimeSaiXuanActivity.this));
        lvComeIn.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvComeIn.setAdapter(adapter);
        adapter.setOnItemsClistener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CubeBean bean = adapter.getList().get(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("type", "0");
                map.put("starttime", starttime);
                map.put("endtime", endtime);
                JumpUtils.jumpto(TimeSaiXuanActivity.this, TurnoverActivity.class, (Serializable) bean, map);
            }
        });
    }

    @Override
    public void initRecy1Project(List<CubeBean> listproject) {
        final StockAdapter adapter = new StockAdapter(TimeSaiXuanActivity.this, listproject);
        adapter.setType(1);
        lvComeIn2.addItemDecoration(new DividerGridItemDecoration(TimeSaiXuanActivity.this));
        lvComeIn2.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvComeIn2.setAdapter(adapter);
        adapter.setOnItemsClistener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CubeBean bean = adapter.getList().get(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("type", "1");
                map.put("starttime", starttime);
                map.put("endtime", endtime);
                JumpUtils.jumpto(TimeSaiXuanActivity.this, TurnoverActivity.class, (Serializable) bean, map);
            }
        });
    }

    @Override
    public void initRecy2ZhiChu(List<CubeBean> listproject) {
        final StockAdapter adapter = new StockAdapter(TimeSaiXuanActivity.this, listproject);
        adapter.setType(2);
        lvOutMoney.addItemDecoration(new DividerGridItemDecoration(TimeSaiXuanActivity.this));
        lvOutMoney.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvOutMoney.setAdapter(adapter);
        adapter.setOnItemsClistener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                CubeBean bean = adapter.getList().get(i);
//                HashMap<String, String> map = new HashMap<>();
//                map.put("type", "2");
//                JumpUtils.jumpto(getActivity(), TurnoverActivity.class, (Serializable) bean, map);
            }
        });
    }

    List<Integer> List_img;


    TurnoverBean bean;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    bean = JsonUtil.getEntityByJsonString(data.toString(), TurnoverBean.class);
                    cubeBeanList = ServiceListBeantoCubeBean(bean.getServiceList());
                    cubeBeanList2 = ProjectlistBeantoCubeBean(bean.getProjectlist());
                    cubeBeanList3 = ZhiChulistBeantoCubeBean(bean.getZhiChuList());
                    tvTopYingyee.setText("" + bean.getYingyee());
                    tvAllGet.setText("+" + bean.getTotalShouru() + "元");
                    tvAllZhichu.setText("-" + bean.getTotalZhichu() + "元");
                    tvJinghuoxiaofei.setText("" + bean.getJinhuoXiaofei() + "元");
                    initRecy0Service(cubeBeanList);
                    initRecy1Project(cubeBeanList2);
                    initRecy2ZhiChu(cubeBeanList3);
                    flSeeMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            HashMap<String, String> map = new HashMap<>();
                            map.put("type", "0");
                            map.put("starttime", starttime);
                            map.put("endtime", endtime);
                            JumpUtils.jumpto(TimeSaiXuanActivity.this, ServiceOrProductListActivity.class, map);
                        }
                    });
                    flSeeMore2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            HashMap<String, String> map = new HashMap<>();
                            map.put("type", "1");
                            map.put("starttime", starttime);
                            map.put("endtime", endtime);
                            JumpUtils.jumpto(TimeSaiXuanActivity.this, ServiceOrProductListActivity.class, map);
                        }
                    });
                    flSeeMore3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            HashMap<String, String> map = new HashMap<>();
                            map.put("type", "2");
                            map.put("starttime", starttime);
                            map.put("endtime", endtime);
                            JumpUtils.jumpto(TimeSaiXuanActivity.this, ServiceOrProductListActivity.class, map);
                        }
                    });
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @OnClick(R.id.tv_time_saixuan)
    public void onViewClicked() {
        JumpUtils.jumpto(TimeSaiXuanActivity.this, TimeSaiXuanActivity.class, null);
//        R.id.tv_time_saixuan
    }

    @Override
    public void update(Observable observable, Object o) {

//        if (time.isEmptys()) {
//            svScrollview.setVisibility(View.GONE);
//        } else {
//            svScrollview.setVisibility(View.VISIBLE);
//            getNetData();
//        }


    }

    interface initData {

        void getNetData();

        List<CubeBean> ServiceListBeantoCubeBean(List<TurnoverBean.ServiceListBean> beans);

        List<CubeBean> ProjectlistBeantoCubeBean(List<TurnoverBean.ProjectlistBean> beans);

        void initRecy0Service(List<CubeBean> listservice);

        void initRecy1Project(List<CubeBean> listproject);
    }
}